﻿using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using Web.Pay.Models;
using System.Web.Mvc;
using Web.Base.Security;
using Web.Base.Extensions;
using Ayatta.Contract.Extension;
using Ayatta.Contract.Component;

namespace Web.Pay.Controllers
{
    public class GameController : PayBaseController
    {
        private string resultUrl = "/recharge/result/";
        public GameController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        /// <summary>
        /// 游戏充值
        /// </summary>
        /// <param name="param">游戏区服Id</param>
        /// <param name="gameId">游戏Id</param>
        /// <returns></returns>
        public ActionResult Index(int? param, int? gameId)
        {
            var identity = User.Identity.Identity();
            var model = new GameControllerModel.Index();
            var games = GetGames();
            var data = PassportService.GetUserExtra(identity.Id).GameData();
            var played = games.Where(o => data.Keys.Contains(o.Id)).ToList();
            model.Games = games;
            model.Played = played;
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(int gameId, int serverId, decimal amount, string to)
        {
            var identity = User.Identity.Identity();
            var result = new SkResult<string>();
            var games = GetGames();
            var data = PassportService.GetUserExtra(identity.Id).GameData();

            if (data.ContainsKey(gameId))
            {
                var game = games.FirstOrDefault(o => o.Id == gameId);
                var client = GetGameClient(gameId);
                var server = games.GetAllServers().FirstOrDefault(o => o.Id == serverId);
                if (server != null)
                {
                    var temp = client.CheckUser(identity.Guid, server.Params);
                    if (temp != null && temp.Status)
                    {
                        var order = CreateGameOrder(game, server, amount, identity.Id, identity.NickName, User.Identity.Name, GetIpAddress());
                        var status = OrderService.CreateGameOrder(order);
                        result.Status = status;
                        if (!status)
                        {
                            result.Message = "创建充值订单失败！";
                        }
                    }
                    else
                    {
                        result.Message = "请先登录游戏创建角色！";
                    }
                }
                else
                {
                    result.Message = "参数错误！";
                }
            }
            else
            {
                result.Message = "请先登录游戏创建角色！";
            }
            return Json(result);
        }

        /// <summary>
        /// 检查是否登录过游戏
        /// </summary>
        /// <param name="gameId">游戏Id</param>
        /// <param name="username">用户名</param>
        /// <returns></returns>
        public ActionResult Check(int gameId, string username)
        {
            var identity = User.Identity.Identity();
            var result = new SkResult<string>();
            if (!Request.IsAuthenticated)
            {
                result.Message = "请先登录！";
                return Json(result);
            }
            var data = PassportService.GetUserExtra(identity.Id).GameData();
            if (!data.ContainsKey(gameId))
            {
                result.Message = "您还未登录过该游戏！";
                return Json(result);
            }
            return Json(result);
        }
    }
}
